<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 登录表单区 -->
      <el-form ref="loginFormRef" :model="loginForm" class="loginForm" :rules="loginRules">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-ren"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="loginForm.password" prefix-icon="iconfont icon-suo1"></el-input>
        </el-form-item>
        <!-- 按钮区 -->
        <el-form-item label-width="278px">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "",
  created() {},
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456"
      },
      loginRules:{
           username: [
            { required: true, message: '用户名不能为空', trigger: 'blur' },
            { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' },
            { min: 5, max: 15, message: '长度在 5 到 15 个字符', trigger: 'blur' }
          ],
      }
    };
  },
  methods: {
    login() {
        this.$refs.loginFormRef.validate((isok)=>{
            if(!isok){
                return;
            }
            this.$http.post('login',this.loginForm).then((res)=>{
                console.log(res);
                var {meta,data} = res.data
                if(meta.status == 200){
                    this.$message({message:meta.msg , type: "success" });                     
                    //登录成功后服务器返回的token,保存到sessionStorage中                  
                    window.sessionStorage.setItem('token',data.token);
                    //顺序不能倒,先要保存token然后再跳转
                    this.$router.push('/home')
                }else{
                    this.$message.error(meta.msg); 
                }
            })
        })
    },
    // 重置表单
    resetLoginForm() {
        this.$refs.loginFormRef.resetFields()
    }
  },
  components: {}
};
</script>

<style lang='less' scpoed>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
  .login_box {
    width: 450px;
    height: 280px;
    background-color: #fff;
    border-radius: 5px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .avatar_box {
      width: 100px;
      height: 100px;
      border: 1px solid #eee;
      background-color: #fff;
      border-radius: 50%;
      padding: 5px;
      box-shadow: 0 0 5px #ddd;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      img {
        widows: 100px;
        height: 100px;
        border-radius: 50%;
        background-color: #eee;
      }
    }
  }
}
.loginForm {
  padding: 96px 10px 20px;
}
</style>
